from openai import OpenAI
API_BASE_URL = "https://api.deepseek.com"
API_KEY = "sk-217b15a7b9854cc9ad605d1c1ca49b98"


class LLMClient:
    def __init__(self, api_key=API_KEY, base_url=API_BASE_URL):
        """初始化LLM客户端"""
        if api_key == "":
            api_key = API_KEY
            print("New api key not found")
            print("Using default api key")
        if base_url == "":
            base_url = API_BASE_URL
            print("New api url not found")
            print("Using default api url")

        self.client = OpenAI(
            api_key=api_key,
            base_url=base_url
        )

    def chat(self, messages, model="deepseek-reasoner", player_name=""):
        """与LLM交互

        Args:
            messages: 消息列表
            model: 使用的LLM模型
            api_key: 使用的API密钥
            base_url: 使用的API基础URL

        Returns:
            tuple: (content, reasoning_content)
        """
        try:
            # print(f"LLM请求: {messages}")
            response = self.client.chat.completions.create(
                model=model,
                messages=messages,
            )
            if response.choices:
                message = response.choices[0].message
                content = message.content if message.content else ""
                reasoning_content = getattr(message, "reasoning_content", "")
                print(f"<{player_name}>LLM推理内容: {content}")
                return content, reasoning_content

            return "", ""

        except Exception as e:
            print(f"<{player_name}>LLM调用出错: {str(e)}")
            return "", ""

def get_():
    import requests

    url = "https://api.deepseek.com/user/balance"

    payload={}
    headers = {
        'Accept': 'application/json',
        'Authorization': 'Bearer <TOKEN>'
    }

    response = requests.request("GET", url, headers=headers, data=payload)

    print(response.text)


# 使用示例
if __name__ == "__main__":
    llm = LLMClient()

    messages = [
        {"role": "user", "content": "你好"}
    ]
    response = llm.chat(messages)
    print(f"响应: {response}")